import axios from 'axios'
import md5 from 'js-md5'
import { getItem,setItem,randomString,inDeepcoinApp } from '@/util';
import { apiUrl } from '@/api/url';
import { appKey } from '@/api/enmu';
import store from '@/store/public';
import { message } from 'antd';

//生成设备ID
let _devicePrefix = 'pc-';
let device = getItem('device') || _devicePrefix + md5(+new Date() + window.navigator.userAgent + randomString())
if (!getItem('device')) setItem('device', device)

//axios 配置
axios.defaults.timeout = 1000 * 10;
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
axios.defaults.headers.get['Content-Type'] = 'application/x-www-form-urlencoded'

//不提示异常接口列表
let noRemindList = [
  'common/config',
  'site/config',
  'v1.0/kline',
  'common/price-by-option',
  'v1.0/marketdata',
  'notice/system-notice',
  'common/symbol-list',
  'currency/currencyfavor/favor-list',
  'query/v1.0/position',
  'query/v1.0/order',
  'query/v1.0/trigger_order',
  'query/v2.0/finish-orders',
  'query/v2.0/finish-trigger-orders',
  'per-order/get-track-list',
  'query/v2.0/trades',
  'spotquery/v1.0/SendBatch',
  'common/price-list',
  'spotquery/v1.0/account',
  'common/rank/list',
  'common/statistic/st',
  'welfare/center/tasks',
  'query/v1.0/SendBatch',
  'per-order/get-track-price',
  'country/unlogined-access',
  'country/logined-access',
  'country/login-check',
  'common/functional-entrace',
  'common/notice',
  'helpcenter/helpcenter/new-menu',
  'common/currency-rate',
  'language/language/list',
  'site/get-notice',
  'common/guide/list',
  'site/new-get-banner',
  'site/spot-instrument-list',
  'common/list-symbol-by-label',
  'common/product/list',
]

// console.log(store.locale)
// request 拦截器
axios.interceptors.request.use(
    (config) => {
        const token = store.token;
        const locale = store.locale === 'zh-CN'?'zh':store.locale;
        if (inDeepcoinApp()) {
            config.headers['platform'] = 'h5'
            config.headers['lang'] = '';
            config.headers['token'] = '';
            config.headers['device'] = device;
        } else {
            config.headers['timestamp'] = +new Date()
            config.headers['device'] = device
            config.headers['platform'] = 'pc'
            config.headers['lang'] = locale;
            if (token) config.headers['token'] = token
            if (token) config.headers['Otoken'] = token
            if (store.uid) config.headers['uid'] = store.uid;
            config.headers['appid'] = appKey.appid
        }
        config.headers['version'] = process.env.REACT_APP_VERSION
        config.headers['requestid'] = md5(+new Date() + window.navigator.userAgent + randomString())
        return config
    },
    (err) => Promise.reject(err)
)

// response 拦截器
axios.interceptors.response.use(
  (response) => {
        const { data, status, config } = response;
        if (status === 200) {
            if (data && data.status == 'success' && data.result == 'updating') {
                return data
            }
            let { retCode, retData, retMsg, code, msg } = data || {}
            if (
                retCode === 100002 ||
                retCode === 1000010 ||
                code === 'NotLoggedError' ||
                code == 10000009 ||
                (response.request.responseURL.includes(apiUrl.partnerUrl) && code === 5100)
            ) {
                // token无效
            }
            return data
        } else {
            
        }
        return data
    },
    (err) => {
        if(err && err.message){
            message.warning(err.message)
        }
    }
)

const $http = {
    post(_url,params){
        return axios.post(_url,params)
    }
}

export default $http